package com.troll.wanandroid.design.fragment

import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.troll.base.BaseFragment
import com.troll.wanandroid.R
import com.troll.wanandroid.adapter.system.SystemAdapter
import com.troll.wanandroid.viewmodel.SystemViewModel

/**
 *
 * @Author:   TangPeng
 * @Time:     6/10/21
 * @Remark:  体系
 */
class SystemFragment : BaseFragment<SystemViewModel>() {

    private var rv: RecyclerView? = null
    private var adapter: SystemAdapter? = null

    override val layoutId: Int
        get() = R.layout.frag_system

    override fun flowOfData() {
        rv = findViewById(R.id.system_rv)
        adapter = SystemAdapter();
        rv?.layoutManager = StaggeredGridLayoutManager( 4,StaggeredGridLayoutManager.VERTICAL)
        rv?.adapter = adapter

        findViewById<TextView>(R.id.tv)?.setOnClickListener {
            with(viewModel) {
                knowledgeTree()
            }
        }

        // 请求数据
        with(viewModel) {
            knowledgeTree()
        }

    }

    override fun flowOfSetup() {
        super.flowOfSetup()

        with(viewModel) {
            liveKnowledgeTree.observe(this@SystemFragment) {
                adapter?.data?.clear()
                adapter?.data = it
                adapter?.notifyDataSetChanged()
            }
        }
    }
}